<?php

class Pagination {
	
	var $url;
	var $recordsToShow;
	var $pagesToShow;
	var $display;
	var $parametres;
	var $start;
	var $end;
	var $currentPage;
	var $totalRecords;
	var $refPage;
	var $method = false;
	var $id;
	
	/**
	 * @author Diego
	 */
	function Pagination($url, $recordsToShow=20, $pagesToShow=10, $display=array())
	{
		$this->url =  $url;
		$this->recordsToShow = (is_integer($recordsToShow) && $recordsToShow > 0 ? $recordsToShow : 20);
		$this->pagesToShow = (is_integer($pagesToShow) && $pagesToShow > 0 ? $pagesToShow : 10);
		
		$this->display = array(
			'firstPageClass' => 'firstPage',
			'firstPageLabel' => '&laquo;',
			'previousPageClass' => 'previousPage',
			'previousPageLabel' => '&lt;',
			'currentClass' => 'current',
			'pageClass' => 'page',
			'nextPageClass' => 'nextPage',
			'nextPageLabel' => '&gt;',
			'lastPageClass' => 'lastPage',
			'lastPageLabel' => '&raquo;',
			'goToClass' => 'goToPage',
			'goToLabel' => 'Ir para a p&aacute;gina ',
			'goToInputName' => 'goToPageInput',
			'goToInputId' => 'goToPageInput',
			'goToInputClass' => 'goToPageInput',
			'goToOfLabel' => ' de ',
			'totalPagesClass' => 'totalPages',
			'goToButton' => '<button type="submit">Ir</button>'
		);
		$this->parametre = array();
		$this->setDisplay($display);
	}
	
	function setId($id)
	{
		$this->id = $id;
	}
	function setParametrePage($page)
	{
		$this->refPage = $page;
	}
	
	function setRecordsToShow($recordsToShow)
	{
		if (is_integer($recordsToShow))
			$this->recordsToShow;
	}
	
	function setPage($currentPage, $totalRecords)
	{
		$this->currentPage = $currentPage;
		$this->totalRecords = $totalRecords;
	}
	
	function setPagesToShow($pagesToShow)
	{
		if (is_integer($pagesToShow))
			$this->pagesToShow;
	}
	
	function setDisplay($display)
	{
		if (is_array($display))
			foreach ($display as $key => $value) {
				if (array_key_exists($key, $this->display))
					$this->display[$key] = $value;
			}
	}
	
	function setParametre($value)
	{
		$this->parametres = $value;
	}
	
	function setPost($metodo = false)
	{
		$this->method = $metodo;
	}
	
	function mount() 
	{
		$html = array(
			'first' => '',
			'previous' => '',
			'pages' => '',
			'next' => '',
			'goTo' => ''
		);	
		// Determina o n�mero da �ltima p�gina
		$lastPage =  floor($this->totalRecords / $this->recordsToShow) + ($this->totalRecords % $this->recordsToShow != 0 ? 1 : 0);
			
		if ($lastPage < 2) return $html;
	
		if($this->method == false)
		{			
			if(is_array($this->parametres ))
			{
				$parametro = "";			
				foreach($this->parametres as $key => $value)
				{
					$parametro .= "/" .$key.":". $value;			
				}
			}
		}
		else
		{
			$html['parameter'] = $this->parametres;
		}
		
		$html['action'] = $this->url;	
		$this->url = $this->url . $parametro;	
		
		$middle = $this->pagesToShow / 2;
		$pages = array_fill(0, $this->pagesToShow, null);
		
		if ($this->currentPage != 1) 
		{
			if(!$this->isPost())
$html['first'] = '<span class="'.$this->display['firstPageClass'].'"><a href="'.$this->url. "/". $this->refPage.':1">'.$this->display['firstPageLabel'].'</a></span>';
			else
$html['first'] = '<span class="'.$this->display['firstPageClass'].'"><a href="#" onclick="Pag.send(\''.$this->id.'\',\''.$this->refPage.'\','. 1 .')">'.$this->display['firstPageLabel'].'</a></span>';
		
		}
		
		if ($this->currentPage > 1) {
			if(!$this->isPost())
$html['previous'] = '<span class="'.$this->display['previousPageClass'].'"><a href="'.$this->url. "/". $this->refPage.':'.($this->currentPage - 1) .'">'.$this->display['previousPageLabel'].'</a></span>';
		else
$html['previous'] = '<span class="'.$this->display['previousPageClass'].'"><a href="#" onclick="Pag.send(\''.$this->id.'\',\''.$this->refPage.'\','. ($this->currentPage - 1) .')">'.$this->display['previousPageLabel'].'</a></span>';
		
		}
		
		if ($lastPage > ($this->pagesToShow + 1)) {
			
			$halfPagesShow = $this->pagesToShow / 2;
			
		    if ($this->currentPage < $halfPagesShow) {
		        // Mediano � esquerda
		    	$middle = $this->currentPage - 1;
		    } else if ($this->currentPage > ($lastPage - $halfPagesShow)) {
		        // Mediano � direita
		    	$middle = $this->pagesToShow - ($lastPage - $this->currentPage);
		    }
		} else {
		    // Tamanho menor que m�ximo de exibi��es, middle ajustado
		    $middle = $this->currentPage - 1;
		}
		
		// Orienta��o decrescente
		for ($i = ($middle == $this->currentPage ? $middle-- : $middle); $i > -1 && $this->currentPage - ($middle - $i) >= $this->currentPage - $middle; $i--) {
		    $pages[$i] = $this->currentPage - ($middle - $i);
		}
		
		// Orienta��o crescente
		for ($i = $middle; $i < ($this->pagesToShow + 1) && $this->currentPage + ($i - $middle) <= $lastPage; $i++) {
		    $pages[$i] = $this->currentPage + ($i - $middle);
		}
		
		for ($i = 0; $i < count($pages); $i++) {
			if (is_integer($pages[$i])) {
				$current = ($pages[$i] == $this->currentPage ? ' '.$this->display['currentClass'] : '');
				
				if(!$this->isPost())
$html['pages'][$i] = '<span class="'.$this->display['pageClass'].$current.'"><a href="'.$this->url. "/". $this->refPage.':'.$pages[$i].'">'.$pages[$i].'</a></span>';
				else
$html['pages'][$i] = '<span class="'.$this->display['pageClass'].$current.'"><a href="#"  onclick="Pag.send(\''.$this->id.'\',\''.$this->refPage.'\','.  $pages[$i] .')">'.$pages[$i].'</a></span>';
			
			}
		}
		
		if ($this->currentPage < $lastPage) {
			if(!$this->isPost())
$html['next'] = '<span class="'.$this->display['nextPageClass'].'"><a href="'.$this->url."/". $this->refPage.':'.($this->currentPage + 1).'">'.$this->display['nextPageLabel'].'</a></span>';
			else
$html['next'] = '<span class="'.$this->display['nextPageClass'].'"><a href="#"  onclick="Pag.send(\''.$this->id.'\',\''.$this->refPage.'\','.  ($this->currentPage + 1) .')">'.$this->display['nextPageLabel'].'</a></span>';	
		}
		
		if ($this->currentPage != $lastPage) {
			if(!$this->isPost())
$html['last'] = '<span class="'.$this->display['lastPageClass'].'"><a href="'.$this->url."/".$this->refPage.':'.$lastPage.'">'.$this->display['lastPageLabel'].'</a></span>';
			else
$html['last'] = '<span class="'.$this->display['lastPageClass'].'"><a href="#"  onclick="Pag.send(\''.$this->id.'\',\''.$this->refPage.'\','. $lastPage .')">'.$this->display['lastPageLabel'].'</a></span>';
		}
		
		$html['goTo'] = '<div class="'.$this->display['goToClass'].'">'.$this->display['goToLabel'].'<input type="text" name="'.$this->refPage.'" id="'.$this->refPage ."_input".'" class="'.$this->display['goToInputClass'].'" value="' . $this->currentPage . '">'.$this->display['goToOfLabel'].'<span class="'.$this->display['totalPagesClass'].'">' . $lastPage . '</span>' . $this->display['goToButton'] . '</div>';
				
		$html['method'] = 'Post';	
		$html['action'] = $this->url . $parametro;		
		$html['id'] = $this->id;		
		
		//DebugUtil::PrintValue($html);exit;
		return $html;
	}
	
	function isPost()
	{
		if($this->method == true)
			return true;
		return false;
	}
}
?>